import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Form} from "antd";
import {query} from "./actions";
import {DATE_FORMAT} from "../../lib/func";
import FilterForm from "../../components/FilterForm";
import {getCurrentOrganization, getLastFilter, getPrincipal, saveLastFilter} from "../../lib/identity";
const LAST_FILTER = "hisZadOrderReceivable";

class Filter extends PureComponent {
    state = {};
    handleSearch = (values) => {
        const {dispatch} = this.props;
        let fields = values;
        this.setState({...fields});
        if (fields.createTimeRange && fields.createTimeRange.length > 1) {
            fields.blTimeStart = fields.createTimeRange[0].format(DATE_FORMAT);
            fields.blTimeEnd = fields.createTimeRange[1].format(DATE_FORMAT);
        }
        fields.userId = getPrincipal().id;
        delete fields.createTimeRange;
        saveLastFilter(LAST_FILTER, {...fields});
        dispatch(query({...fields, "organizationId": getCurrentOrganization(), "type": "IN"}));
    };

    render() {
        const {loading} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        const filterSchema = [
            {
                key: 'organizationName',
                field: 'organizationName',
                type: 'text',
                expandable: true,
                width: 200,
                title: '所属机构',
                fieldOptions: {
                    initialValue: this.state.organizationName || fields.organizationName
                }
            }, {
                key: 'bookNo',
                field: 'bookNo',
                type: 'text',
                expandable: true,
                width: 200,
                title: '订舱号',
                fieldOptions: {
                    initialValue: this.state.bookNo || fields.bookNo
                }
            }, {
                key: 'clientName',
                field: 'clientName',
                type: 'text',
                expandable: true,
                width: 200,
                title: '客户',
                fieldOptions: {
                    initialValue: this.state.clientName || fields.clientName
                }
            }, {
                key: 'packageNo',
                field: 'packageNo',
                type: 'text',
                expandable: true,
                width: 200,
                title: '柜号',
                fieldOptions: {
                    initialValue: this.state.packageNo || fields.packageNo
                }
            }, {
                key: 'createTimeRange',
                field: 'createTimeRange',
                type: 'dateRangePicker',
                expandable: true,
                title: '班列日期',
                fieldOptions: {
                    initialValue: this.state.createTimeRange || fields.createTimeRange
                }
            }
        ];
        return (
            <FilterForm schema={filterSchema} callback={this.handleSearch} loading={loading}/>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.hisZadOrderReceivable.list
    };
};

export default connect(mapStateToProps)(Form.create()(Filter));
